//
//  ShowGroupViewController.m
//  Closer
//
//  Created by zhangkai on 16/3/12.
//  Copyright © 2016年 Closer_All. All rights reserved.
//

#import "ShowGroupViewController.h"
#import <EaseMobSDKFull/EaseMob.h>
#import "ChatViewController.h"
#define CELL_id @"CELL_id"
@interface ShowGroupViewController ()<UITableViewDataSource,UITableViewDelegate,IChatManagerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)backButton:(id)sender;

@property(nonatomic,strong)NSArray  *dataArray;
@end

@implementation ShowGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addDelegate];
    [self getData];
    
}
-(void)addDelegate{
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
}
-(void)getData{
  // [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsList];

    [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
        self.dataArray =groups;
        [self.tableView reloadData];
    } onQueue:nil];
}

-(void)dealloc{
    self.dataArray=nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark -  tableview 代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CELL_id];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CELL_id];
    }
    EMGroup   *Group=self.dataArray[indexPath.row];
    switch (indexPath.section) {
        case 0:
                cell.textLabel.text=Group.groupSubject;
            break;
            
        default:
            break;
    }

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
         EMGroup   *Group=self.dataArray[indexPath.row];
         ChatViewController *chatController=   [[ChatViewController alloc]initWithChatter:Group.groupId isGroup:YES];
        [self presentViewController:chatController animated:YES completion:^{
            
        }];
    }
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString * groupTypeString=nil;
    switch (section) {
        case 0:
           groupTypeString=@"自己建立的群";
            break;
        case 1:
            groupTypeString=@"自己加入的群";
            break;
        default:
            break;
    }
    return groupTypeString;
}
- (IBAction)backButton:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
